<?php
require_once("vcl/vcl.inc.php");
require_once("lib/db_connection.php");
require_once("lib/my_library.php");
//Includes
use_unit("components4phpfull/jtjavascript.inc.php");
use_unit("components4phpfull/jtdatepicker.inc.php");
use_unit("components4phpfull/jtsitetheme.inc.php");
use_unit("components4phpfull/jtgroupbox.inc.php");
use_unit("comctrls.inc.php");
use_unit("forms.inc.php");
use_unit("extctrls.inc.php");
use_unit("stdctrls.inc.php");

//Class definition
class closeWO extends Page
{
       public $hiddenWO = null;
       public $edLaborTot = null;
       public $cmblabordesc = null;
       public $hiddenReminder = null;
       public $Image2 = null;
       public $Label3 = null;
       public $JTDate = null;
       public $cmbperformed = null;
       public $memoNotes = null;
       public $lblNotes = null;
       public $Label2 = null;
       public $chkWarranty = null;
       public $Label1 = null;
       public $edCost = null;
       public $lblCost = null;
       public $edLaborUnits = null;
       public $lblLaborUnits = null;
       public $cmbName = null;
       public $lblPerformed = null;
       public $Image1 = null;
       public $JTSiteTheme1 = null;
       public $hiddenLine = null;
       public $edTax = null;
       public $chkParts = null;
       public $chkLabor = null;
       public $chkMisc = null;
       public $JTGroupBox1 = null;
       public $lblTotal = null;
       public $edTotal = null;
       public $lblTax = null;
       public $lbComputedlTax = null;
       public $edComputedTax = null;
       public $lblDiscount = null;
       public $edDiscount = null;
       public $edParts = null;
       public $lblParts = null;
       public $edMisc = null;
       public $lblMisc = null;
       public $edLabor = null;
       public $lblLabor = null;
       public $jtStatus = null;
       public $chkComplete = null;
       public $chkRecall = null;
       public $chkWarr = null;
       public $memoRemarks = null;
       public $btnCancel = null;
       public $btnSubmit = null;
       public $lblMaint1 = null;
       public $memoFullDescription = null;
       public $lblFullDescription = null;
       public $cmbDescription = null;
       public $lblDescription = null;
       public $cmbAcct = null;
       public $lblAcct = null;
       public $cmbServiced = null;
       public $lblServiced = null;
       public $cmbDriver = null;
       public $edOdometer = null;
       public $lblOdometer = null;
       public $lblDriver = null;
       public $edOrderNo = null;
       public $lblOrderNo = null;
       public $dpCompletionDate = null;
       public $lblCompletionDate = null;
       public $dpCreationDate = null;
       public $lblCreationDate = null;
       function btnCancelClick($sender, $params)
       {
       $this->deleteCancel();

       echo '<script language=javascript>
       window.close();
       </script>';

       }


       /*function deleteCancel()
       {
        $query_delete_wo = "DELETE FROM veh_workorder_tbl WHERE workorder_id='".$this-->getValue()."'";
        mysql_query($query_delete_wo) or die("DELETE WO ".mysql_error());

        $query_delete_laborline = "DELETE FROM veh_laborline_tbl WHERE workorder='".$this->hiddenWO->getValue()."'";
        mysql_query($query_delete_laborline) or die("DELETE LABORLINE ".mysql_error());

        $query_sel_parts = "SELECT parts_id,quantity,part_num FROM veh_parts_tbl WHERE workorder='".$this->hiddenWO->getValue()."'";
        $result_sel_parts = mysql_query($query_sel_parts) or die("SELECT PARTS ".mysql_error());
        while($row_sel_parts = mysql_fetch_array($result_sel_parts))
        {
         $part_id = $row_sel_parts['parts_id'];
         $qty_delete = $row_sel_parts['quantity'];
         $stock_id = $row_sel_parts['part_num'];

         $query_inv = "SELECT qty_available FROM invstocks_tbl WHERE stock_id='".$stock_id."'";
         $result_inv = mysql_query($query_inv) or die('INVENTORY - QUANTITY '.mysql_error());
         $row_inv = mysql_fetch_array($result_inv);
         $curr_qty = $row_inv['qty_available'];

         $new_qty = $curr_qty + $qty_delete;
         $query_up_inv = "UPDATE invstocks_tbl SET qty_available='".$new_qty."' WHERE stock_id='".$stock_id."'";
         mysql_query($query_up_inv) or die("UPDATE INV ".mysql_error());

         $query_select_inv_stat = "SELECT qty_available FROM invstocks_tbl WHERE stock_id='".$stock_id."'";
         $result_select_inv_stat = mysql_query($query_select_inv_stat) or die("SELECT INV STAT ".mysql_error());
         $count_inv_stat = mysql_num_rows($result_select_inv_stat);

         if($count_inv_stat>0)
         {
          $query_up_inv = "UPDATE invstocks_status_tbl SET qty_available='".$new_qty."' WHERE stock_id='".$stock_id."'";
          mysql_query($query_up_inv) or die("UPDATE INV STATUS ".mysql_error());
         }

         $query_delete_parts = "DELETE FROM veh_parts_tbl WHERE parts_id ='".$part_id."'";
         mysql_query($query_delete_parts) or die("DELETE PARTS ".mysql_error());
        }
       }*/

       function btnCancelJSClick($sender, $params)
       {
       ?>
       window.close();
       <?php

       }

       function cmbperformedChange($sender, $params)
       {
       $performed = $this->cmbperformed->getItemIndex();
       if($performed=='employee')
       {
       $this->cmbName->Clear();
       $result_emp = mysql_query("select * from employee_tbl where company_id='".$_SESSION['company_id']."' and job_title='Fleet Technician'");

       while($row_emp = mysql_fetch_array($result_emp))
       {
            $this->cmbName->AddItem($row_emp['lname'].','.$row_emp['fname'], null, $row_emp['lname'].','.$row_emp['fname']);
       }
       }
       else
       {
       $this->cmbName->Clear();
       $result_vendor = mysql_query("select * from vendor_tbl where company_id='".$_SESSION['company_id']."'");

       while($row_vendor = mysql_fetch_array($result_vendor))
       {
            $this->cmbName->AddItem($row_vendor['vendor_name'], null, $row_vendor['vendor_name']);
       }
       }



       }

       function chkMiscJSChange($sender, $params)
       {

       ?>
       //Add your javascript code here
       if(document.getElementById("chkMisc").checked==true)
       {
       document.getElementById("edMisc").disabled=false;
       }

       if(document.getElementById("chkMisc").checked==false)
       {
       document.getElementById("edMisc").disabled=true;
       document.closeWO.edMisc.value = '';

       document.closeWO.edComputedTax.value = Number(document.closeWO.edMisc.value * Number(document.closeWO.edTax.value/100));

       document.closeWO.edTotal.value = Number(document.closeWO.edMisc.value) +
       Number(document.closeWO.edComputedTax.value) -
       Number(document.closeWO.edDiscount.value);
       }
       //this.setWODetails();
       <?php

       }

       function edMiscJSKeyUp($sender, $params)
       {

       ?>
       Add your javascript code here
       document.closeWO.edTotal.value = Number(document.closeWO.edMisc.value) +
       Number(document.closeWO.edComputedTax.value) -
       Number(document.closeWO.edDiscount.value);
       <?php

       }

       function edDiscountJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.closeWO.edTotal.value = Number(document.closeWO.edMisc.value) +
       Number(document.closeWO.edComputedTax.value) -
       Number(document.closeWO.edDiscount.value);
       //this.setWODetails();
       <?php

       }

       function edComputedTaxJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.closeWO.edTotal.value = Number(document.closeWO.edMisc.value) +
       Number(document.closeWO.edComputedTax.value) -
       Number(document.closeWO.edDiscount.value);
       //this.setWODetails();
       <?php

       }

       function edCostJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.closeWO.edLaborTot.value = (document.closeWO.edLaborUnits.value) * (document.closeWO.edCost.value);
       //document.getElementById('edLaborTot').value = (document.getElementById('edLaborUnits').value) * (document.getElementById('edCost').value);
       <?php

       }

       function edLaborUnitsJSKeyUp($sender, $params)
       {

       ?>
       //Add your javascript code here
       document.closeWO.edLaborTot.value = (document.closeWO.edLaborUnits.value) * (document.closeWO.edCost.value);
       //document.getElementById('edLaborTot').value = (document.getElementById('edLaborUnits').value) * (document.getElementById('edCost').value);
       <?php

       }

       function btnSubmitClick($sender, $params)
       {

       //start wo
       $my_library = new my_library();


       if ($this->chkLabor->Checked==true)
       {
       $chkLabor ='1';
       }
       else
       {
       $chkLabor='0';
       }

       if ($this->chkMisc->Checked==true)
       {
       $chkMisc ='1';
       }
       else
       {
       $chkMisc='0';
       }

       if ($this->chkParts->Checked==true)
       {
       $chkParts ='1';
       }
       else
       {
       $chkParts='0';
       }


       if ($this->chkWarr->Checked==true)
       {
       $chkWarr ='1';
       }
       else
       {
       $chkWarr='0';
       }


       if ($this->chkRecall->Checked==true)
       {
       $chkRecall ='1';
       }
       else
       {
       $chkRecall='0';
       }


       if ($this->chkComplete->Checked==true)
       {
       $chkComplete ='1';
       }
       else
       {
       $chkComplete='0';
       }

       $vehicle_id = $this->hiddenLine->getValue();
       $new_wo = $this->generate_workorder();


       $my_library->updateCurrentTrackingValue($vehicle_id, $this->edOdometer->Text);


       $result_wo_id = mysql_query("SELECT MAX(workorder_id) as max FROM veh_workorder_tbl") or die("SELECT WO ID - ".mysql_error());
       $row_wo_id = mysql_fetch_array($result_wo_id);
       $wo_id = $row_wo_id['max']+1;


       mysql_query("INSERT INTO veh_workorder_tbl(vehicle_id,
       creation_date,
       completion_date,
       order_num,
       odometer,
       service_provider,
       account,
       description,
       full_desc,
       maint_custom1,
       labor,
       misc,
       parts,
       discount,
       tax,
       computed_tax,
       total,
       ch_labor,
       ch_misc,
       ch_parts,
       warr,
       backjob,
       complete)

       VALUES
       ('".$this->hiddenLine->getValue()."',
       '".$this->dpCreationDate->Text."',
       '".$this->dpCompletionDate->Text."',
       '".$new_wo."',
       '".$this->edOdometer->Text."',
       '".$this->cmbServiced->getItemIndex()."',
       '".$this->cmbAcct->getItemIndex()."',
       '".$this->cmbDescription->getItemIndex()."',
       '".$this->memoFullDescription->Text."',
       '".$this->memoRemarks->Text."',
       '".$this->edLabor->Text."',
       '".$this->edMisc->Text."',
       '".$this->edParts->Text."',
       '".$this->edDiscount->Text."',
       '".$this->edTax->Text."',
       '".$this->edComputedTax->Text."',
       '".$this->edTotal->Text."',
       '".$chkLabor."',
       '".$chkMisc."',
       '".$chkParts."',
       '".$chkWarr."',
       '".$chkRecall."',
       '".$chkComplete."')") or die("INSERT WORKORDER - ".mysql_error());

       $company_id = $_SESSION['company_id'];
       $result_last_order_num = mysql_query("SELECT last_order_no as last from company_workorders_tbl WHERE
       company_id='".$company_id."'") or die("get last order ".mysql_error());
       $row_lon = mysql_fetch_array($result_last_order_num);
       //var_dump($row_lon);
       $lon = $row_lon['last'];
       $lon++;
       mysql_query("UPDATE company_workorders_tbl SET last_order_no='".$lon."' WHERE
       company_id='".$company_id."'") or die("update last company rec ".mysql_error());

       //end wo



       if($this->chkWarranty->Checked==true)
       {
       $warranty='1';
       }
       else
       {
       $warranty='0';
       }
       $query_get_id = "SELECT last_insert_id(workorder_id) as wo_id FROM veh_workorder_tbl
       ORDER BY workorder_id DESC LIMIT 1";
       $result_get_id = mysql_query($query_get_id) or die("WORKORDER ID ".mysql_error());
       $row_get_id = mysql_fetch_array($result_get_id);
       $wo_id = $row_get_id['wo_id'];


       mysql_query("INSERT INTO veh_laborline_tbl(company_id,
       vehicle_id,
       workorder,
       performed_by,
       employee,
       labor_date,
       labor_unit,
       cost,
       total,
       description,
       notes,
       warranty)

       VALUES
       ('".$_SESSION['company_id']."',
       '".$this->hiddenLine->getValue()."',
       '".$wo_id."',
       '".$this->cmbperformed->getItemIndex()."',
       '".$this->cmbName->getItemIndex()."',
       '".$this->JTDate->Date."',
       '".$this->edLaborUnits->Text."',
       '".$this->edCost->Text."',
       '".$this->edLaborTot->Text."',
       '".$this->cmblabordesc->getItemIndex()."',
       '".$this->memoNotes->Text."',
       '".$warranty."')") or die("INSERT LABORLINE - ".mysql_error());

       // close reminder


       //update veh remind
       mysql_query("UPDATE veh_reminder_tbl set
       status2 = 'Complete'
       WHERE reminder_id='".$this->hiddenReminder->getValue()."'") or die("UPDATE REMINDER - ".mysql_error());
       //end veh remind


       // create new veh_reminder
       $query_rec_reminder = "select * from veh_reminder_tbl where reminder_id='".$this->hiddenReminder->getValue()."'";
       //die($query_rec_reminder);
       $result_rec_query = mysql_query($query_rec_reminder)or die ("veh_remind - ".mysql_error());
       $row_rec_reminder = mysql_fetch_array($result_rec_query);

       $temp_rec_id = $row_rec_reminder['recurring_id'];

       $query_rec = "select * from template_recurring_tbl where id='".$temp_rec_id."'";
       //die($query_rec_reminder);
       $result_rec = mysql_query($query_rec)or die ("veh_remind - ".mysql_error());
       $row_rec = mysql_fetch_array($result_rec);

       $query_veh = "select * from vehicle_tbl where vehicle_id='".$this->hiddenLine->getValue()."'";
       //die($query_rec_reminder);
       $result_veh = mysql_query($query_veh)or die ("vehicle - ".mysql_error());
       $row_veh = mysql_fetch_array($result_veh);


       $flag = $row_rec_reminder['flag_temp'];

       if($flag=="1")
       {

       $sched_by = $row_rec['schedule_by'];

       $temp_od = $row_rec['due_usage'];
       $veh_od = $row_rec_reminder['due_hours'];
       $new_od = $veh_od+$temp_od;


       if($sched_by=="Mileage" || $sched_by=="Hours")
       {
       $odometer = $new_od;
       }

       if($sched_by=="Time")
       {
       $odometer = '0';
       }


       $basis = date('Y-m-d');
       $days_mos = $row_rec['due_by'];
       $val = $row_rec['due_time'];

       if($days_mos=='Days')
       {
       $date = date($basis);
       $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val." day");
       $due_date = date("Y-m-d", $date_computed);
       }

       if($days_mos=='Months')
       {
       $date = date($basis);
       $date_computed = strtotime(date("Y-m-d", strtotime($date)) . "+".$val." month");
       $due_date = date("Y-m-d", $date_computed);
       }

       $result_rec = mysql_query("insert into veh_reminder_tbl (company_id,
       vehicle_id,
       schedule_by,
       due_hours,
       due_date,
       completion_window,
       type,
       description,
       price,
       status2,
       flag_temp,
       recurring_id)

       values ('".$_SESSION['company_id']."',
       '".$this->hiddenLine->getValue()."',
       '".$row_rec_reminder['schedule_by']."',
       '".$odometer."',
       '".$due_date."',
       '".$row_rec_reminder['completion_window']."',
       '".$row_rec_reminder['type']."',
       '".$row_rec_reminder['description']."',
       '".$row_rec_reminder['price']."',
       'Pending',
       '1',
       '".$row_rec['id']."')")or die("insert reminder - ".mysql_error());
       //die($new_od);
       //die($query_ins_remin);
       //$result = mysql_query($query_ins_remin)or die("INSERT REMINDER - " . mysql_error());
       }


       if($flag!="1")
       {
       }

       // end veh_reminder


       echo '<script language=javascript>
       alert("Reminder completed!");
       //window.opener.location.reload();
       //window.top.location.reload();
       window.close()
       window.top.opener.top.location.reload();
       </script>';

       }



       function generate_workorder()
       {
        $company_id = $_SESSION['company_id'];
        $result_wo = mysql_query("SELECT * FROM company_workorders_tbl WHERE company_id='".$company_id."'")
        or die("generate workorder ".mysql_error());
        $row_wo = mysql_fetch_array($result_wo);

        $auto_num = $row_wo['auto_numbering'];

        if($auto_num==1)
        {
        $this->edOrderNo->ReadOnly = true;
        $prefix = $row_wo['prefix'];
        $new_num = $row_wo['last_order_no'] + 1;
        $new_wo = $prefix.'-'.$new_num;
        }
        if($auto_num==0)
        {
        $this->edOrderNo->ReadOnly = false;
        $new_wo = $this->edOrderNo->Text;
        }
        return $new_wo;
       }


       function generate_last_workorder()
       {
        $company_id = $_SESSION['company_id'];
        $result_wo = mysql_query("SELECT * FROM company_workorders_tbl WHERE company_id='".$company_id."'")
        or die("generate workorder ".mysql_error());
        $row_wo = mysql_fetch_array($result_wo);

        $auto_num = $row_wo['auto_numbering'];

        if($auto_num==1)
        {
        $this->edOrderNo->ReadOnly = true;
        $prefix = $row_wo['prefix'];
        $last_num = $row_wo['last_order_no']+1;
        $this->edOrderNo->Text = $prefix.'-'.$last_num;
        }
        if($auto_num==0)
        {
        $this->edOrderNo->ReadOnly = false;
        $this->edOrderNo->Text = '';
        }
       }






       function closeWOCreate($sender, $params)
       {
       $veh_id = $_GET['veh'];
       $reminder = $_GET['reminder'];

       if(isset($veh_id))
       $this->hiddenReminder->setValue($reminder);
       $this->hiddenLine->setValue($veh_id);
       $this->generate_last_workorder();

       $result_reminder = mysql_query("select * from veh_reminder_tbl where reminder_id='".$this->hiddenReminder->getValue()."'");
       $row_reminder = mysql_fetch_array($result_reminder);

       $this->edLabor->Text=$row_reminder['price'];
       //$this->edTotal->Text=$row_reminder['price'];


       //start laborline
       $this->JTDate->setDate(date('Y-m-d'));
       $this->edLaborUnits->Text="1";
       $this->edCost->Text=$row_reminder['price'];
       $this->edLaborTot->Text=$row_reminder['price'];
       $this->memoNotes->Text="&nbsp";

       $this->cmblabordesc->Clear();
       $this->cmblabordesc->setItemIndex($row_reminder['description']);
        $result_desc = mysql_query("select value from lookup_workorder_tbl where category='task descriptions'");

        while($row_desc = mysql_fetch_array($result_desc))
        {
            $this->cmblabordesc->AddItem($row_desc['value'], null, $row_desc['value']);
        }


       //end laborline



       //start wo
       $this->cmbDriver->Clear();
       $result_driver = mysql_query("select * from employee_tbl where company_id='".$_SESSION['company_id']."' and job_title='Driver'");

       while($row_driver = mysql_fetch_array($result_driver))
       {
            $this->cmbDriver->AddItem($row_driver['lname'].','.$row_driver['fname'], null, $row_driver['lname'].','.$row_driver['fname']);
       }

       $this->cmbAcct->Clear();
        $result_account = mysql_query("select value from lookup_expense_tbl where category='accounts'");

        while($row_account = mysql_fetch_array($result_account))
        {
            $this->cmbAcct->AddItem($row_account['value'], null, $row_account['value']);
        }

        $this->cmbDescription->Clear();
        $this->cmbDescription->setItemIndex($row_reminder['type']);
        $result_desc = mysql_query("select value from lookup_workorder_tbl where category='descriptions'");

        while($row_desc = mysql_fetch_array($result_desc))
        {
            $this->cmbDescription->AddItem($row_desc['value'], null, $row_desc['value']);
        }

        $this->cmbServiced->Clear();
        $result_ServiceProvider = mysql_query("select value from lookup_workorder_tbl where category='serviced by'");

        while($row_ServiceProvider = mysql_fetch_array($result_ServiceProvider))
        {
            $this->cmbServiced->AddItem($row_ServiceProvider['value'], null, $row_ServiceProvider['value']);
        }


       $this->dpCreationDate->Text=date('Y-m-d');
       $this->dpCompletionDate->Text=date('Y-m-d');
       $this->edOdometer->Text="";
       $this->memoFullDescription->Text="&nbsp;";
       $this->memoRemarks->Text="&nbsp;";
       //$this->edLabor->Text="";
       $this->edMisc->Text="";
       $this->edParts->Text="";
       $this->edDiscount->Text="";
       $this->edTax->Text="";
       $this->edComputedTax->Text="";
       //$this->edTotal->Text ="";

       $this->edTax->Hidden=true;
       $this->lblTax->Hidden=true;
       $this->chkLabor->Hidden=true;
       $this->chkMisc->Hidden=true;
       $this->chkParts->Hidden=true;

       $result_odometer = mysql_query("select * from vehicle_tbl where vehicle_id='".$this->hiddenLine->getValue()."'");
       $row_od = mysql_fetch_array($result_odometer);
       $this->edOdometer->Text=$row_od['curr_tracking_value'];


       $label_od = $row_od['track_by'];
       if ($label_od=='distance')
        {
        $this->lblOdometer->Caption = 'Odometer:';
        }
       else
        {
        $this->lblOdometer->Caption = 'Clock Hrs:';
        }


       $result_order = mysql_query("select * from company_workorders_tbl where company_id='".$_SESSION['company_id']."'");
       $row_order = mysql_fetch_array($result_order);
       //$this->edOrderNo->Text=$row_order['prefix'].$row_order['last_order_no'];

       $result_billing =mysql_query("select * from company_billing_tbl where company_id='".$_SESSION['company_id']."'");
       $row_billing = mysql_fetch_array($result_billing);
       $label_billing = $row_billing['automatically_calculate'];
       if ($label_billing=='1')
       {
       $this->edTax->Hidden=false;
       $this->lblTax->Hidden=false;
       $this->edTax->Text=$row_billing['percent'];
       $this->chkLabor->Hidden=false;
       $this->chkLabor->Caption="";
       $this->chkMisc->Hidden=false;
       $this->chkMisc->Caption="";
       $this->chkParts->Hidden=false;
       $this->chkParts->Caption="";
       }
       else
       {
       $this->edTax->Hidden=true;
       $this->lblTax->Hidden=true;
       $this->chkLabor->Hidden=true;
       $this->chkMisc->Hidden=true;
       $this->chkParts->Hidden=true;
       }

       $label_billing = $row_billing['labor'];
       if($label_billing=='1')
       {
       $this->chkLabor->Checked=true;
       }
       else
       {
       $this->chkLabor->Checked=false;
       }

       $label_billing = $row_billing['labor'];
       if($label_billing=='1')
       {
       $this->chkLabor->Checked=true;
       }
       else
       {
       $this->chkLabor->Checked=false;
       }

       $label_billing = $row_billing['miscellaneous'];
       if($label_billing=='1')
       {
       $this->chkMisc->Checked=true;
       }
       else
       {
       $this->chkMisc->Checked=false;
       }

       $label_billing = $row_billing['parts'];
       if($label_billing=='1')
       {
       $this->chkParts->Checked=true;
       }
       else
       {
       $this->chkParts->Checked=false;
       }


       //$misc_check = $this->chkMisc->Checked;
       //if($misc_check==true)
       //$this->edMisc->Enabled = true;
       //if($misc_check==false)
       //$this->edMisc->Enabled = false;
       //end wo
       }

}

global $application;

global $closeWO;

//Creates the form
$closeWO=new closeWO($application);

//Read from resource file
$closeWO->loadResource(__FILE__);

//Shows the form
$closeWO->show();

?>